<?php
/**
 * <i>@if</i>组件
 * 
 * 根据条件判断是否显示内容
 * 
 * 可以使用的参数有：
 * - x-condition         string 条件或者是要判断的变量名
 * - [x-condition-value] string 要判断的变量值，常量
 * 
 * 实例:
 *  模板：
 *  <code>
 * <span com="@if" x-condition="$integer>2000">
 * i am aa
 * </span>
 * </code>
 *
 * @version $Id: IMintIfComponent.php 312 2008-04-23 16:29:38Z iwind $
 * @package util.mint.component
 */
class IMintIfComponent extends IAbstractMintComponent {
	/**
	 * 取得支持的参数列表
	 *
	 * @return array
	 */	
	public function getSupportedParameters() {
		return array("x-condition", "x-condition-value");
	}
	
	/**
	 * 取得HTML属性映射
	 *
	 * @return array
	 * @since 0.0.3
	 */		
	public function getHtmlAttrMapping() {
		return array();
	}	
	
	/**
	 * 处理组件
	 *
	 * @return boolean
	 */	
	public function processComponent() {
		$parameters = $this->getParameters();
		
		if (!isset($parameters["x-condition"])) {
			$this->throwUndefinedValueException("x-condition");
		}
		
		//x-condition
		$xCondition = IMintVariable::getDefinition($parameters["x-condition"]);
		if ($xCondition == null) {
			$xCondition = $parameters["x-condition"];
			foreach (IMintConfig::$variableMarkArray as $mark) {
				$mark = preg_quote($mark, "/");
				$xCondition = preg_replace("/({$mark})([a-z_]\\w*)/i", "\$TDataSet->\\2", $xCondition);
				$xCondition = preg_replace("/({$mark})\\{([a-z_]\\w*)\\}/i", "\$TDataSet->\\2", $xCondition);
			}
		}
		
		if (!isset($parameters["x-condition-value"])) {
			$beginText = "<?php if({$xCondition}) { ?>";
		}
		else {
			$xConditionValue = IMintVariable::getDefinition($parameters["x-condition-value"]);
			if ($xConditionValue == null) {//not a variable
				if (!is_numeric($parameters["x-condition-value"])) {
					$xConditionValue = "\"" . addslashes($parameters["x-condition-value"]) . "\"";
				}
				else {
					$xConditionValue = $parameters["x-condition-value"];
				}
			}
			
			$beginText = "<?php if (" . $xCondition .  "==" .  $xConditionValue . ") { ?>";
		}
		$endText = "<?php } ?>";
		$this->addAroundScript($beginText, $endText);
		$this->removeNode();
		return true;
	}
}

?>